package com.arvin.service.serviceImpl;

import com.arvin.mapper.ExcelExportMapper;
import com.arvin.pojo.ExamInfo;
import com.arvin.pojo.ExemptInfo;
import com.arvin.pojo.IntvInfo;
import com.arvin.pojo.ScoreInfo;
import com.arvin.service.ExcelExportService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ExcelExportServiceImpl implements ExcelExportService {

    @Autowired
    private ExcelExportMapper excelExportMapper;
    /**
     * 导出笔试人员信息表
     * @param response
     */
    public void exportExamTable(HttpServletResponse response,String examState,String className){
        //从数据库中获取数据
        List<ExamInfo> examInfoList = excelExportMapper.getExportExamTable(examState,className);
        //通过POI将数据写入Excel文件中
        InputStream resource = this.getClass().getClassLoader().getResourceAsStream("excelTemplate/考试人员信息表模板.xlsx");

        //基于模板文件新建一个新的excel文件

        try {
            XSSFWorkbook excel = new XSSFWorkbook(resource);
            System.out.println(excel);
            //填充数据
            XSSFSheet sheet = excel.getSheet("考试人员信息表");
            Row row = sheet.getRow(0);
            int i = 2;
            for (ExamInfo examInfo:examInfoList){
                row = sheet.createRow(i++);
                System.out.println(examInfo.getStudentId());
                row.createCell(0).setCellValue(examInfo.getStudentId());
                row.createCell(1).setCellValue(examInfo.getStuName());
                row.createCell(2).setCellValue(examInfo.getStuSex());
                row.createCell(3).setCellValue(examInfo.getStuDepartment());
                row.createCell(4).setCellValue(examInfo.getStuGrade());
                row.createCell(5).setCellValue(examInfo.getYear()+"级");
                row.createCell(6).setCellValue(examInfo.getStuSpeciality());
                row.createCell(7).setCellValue(examInfo.getClassNumber()+"班");
                row.createCell(8).setCellValue(examInfo.getExamState());
                if (!StringUtils.isEmpty(examInfo.getExamScore()))
                    row.createCell(9).setCellValue(examInfo.getExamScore());

            }
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);

            outputStream.close();
            excel.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 导出免试面试成绩表
     * @param response
     * @param exemState
     */
    public void exportExemIntvTable(HttpServletResponse response, String exemState,String className) {
        List<ExemptInfo> exemptInfoList = excelExportMapper.getExportExemIntvTable(exemState,className);
        //基于模板文件新建一个新的excel文件
        InputStream resource = this.getClass().getClassLoader().getResourceAsStream("excelTemplate/免试人员信息表模板.xlsx");
        try {
            XSSFWorkbook excel = new XSSFWorkbook(resource);
            System.out.println(excel);
            //填充数据
            XSSFSheet sheet = excel.getSheet("免试人员信息表");
            Row row;
            int i = 2;
            for (ExemptInfo exemptInfo:exemptInfoList){
                row = sheet.createRow(i++);
                System.out.println(exemptInfo.getStudentId());
                row.createCell(0).setCellValue(exemptInfo.getStudentId());
                row.createCell(1).setCellValue(exemptInfo.getStuName());
                row.createCell(2).setCellValue(exemptInfo.getStuSex());
                row.createCell(3).setCellValue(exemptInfo.getStuDepartment());
                row.createCell(4).setCellValue(exemptInfo.getStuGrade());
                row.createCell(5).setCellValue(exemptInfo.getYear()+"级");
                row.createCell(6).setCellValue(exemptInfo.getStuSpeciality());
                row.createCell(7).setCellValue(exemptInfo.getClassNumber()+"班");
                row.createCell(8).setCellValue(exemptInfo.getExemState());
                if (!StringUtils.isEmpty(exemptInfo.getIntvScore()))
                    row.createCell(9).setCellValue(exemptInfo.getIntvScore());

            }
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);

            outputStream.close();
            excel.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 导出面试成绩表
     * @param response
     * @param intvState
     */
    public void exportIntvTable(HttpServletResponse response, String intvState,String className) {
        List<IntvInfo> intvInfoList = excelExportMapper.getExportIntvTable(intvState,className);
        InputStream resource = this.getClass().getClassLoader().getResourceAsStream("excelTemplate/面试人员信息表模板.xlsx");

        //基于模板文件新建一个新的excel文件

        try {
            XSSFWorkbook excel = new XSSFWorkbook(resource);
            System.out.println(excel);
            //填充数据
            XSSFSheet sheet = excel.getSheet("面试人员信息表");

            Row row = sheet.getRow(0);
//            row.getCell(0).setCellValue(1+"入班人员信息表");
            int i = 2;
            for (IntvInfo intvInfo:intvInfoList){
                row = sheet.createRow(i++);
                System.out.println(intvInfo.getStudentId());
                row.createCell(0).setCellValue(intvInfo.getStudentId());
                row.createCell(1).setCellValue(intvInfo.getStuName());
                row.createCell(2).setCellValue(intvInfo.getStuSex());
                row.createCell(3).setCellValue(intvInfo.getStuDepartment());
                row.createCell(4).setCellValue(intvInfo.getStuGrade());
                row.createCell(5).setCellValue(intvInfo.getYear()+"级");
                row.createCell(6).setCellValue(intvInfo.getStuSpeciality());
                row.createCell(7).setCellValue(intvInfo.getClassNumber()+"班");
                row.createCell(8).setCellValue(intvInfo.getIntvState());
                if (!StringUtils.isEmpty(intvInfo.getIntvScore()))
                    row.createCell(9).setCellValue(intvInfo.getIntvScore());

            }
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);

            outputStream.close();
            excel.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 导出综合成绩表
     * @param response
     */
    public void exportScoreTable(HttpServletResponse response,String className) {
        List<ScoreInfo> scoreInfoList = excelExportMapper.getExportScoreTable(className);
        InputStream resource = this.getClass().getClassLoader().getResourceAsStream("excelTemplate/人员成绩信息表模板.xlsx");

        //基于模板文件新建一个新的excel文件

        try {
            XSSFWorkbook excel = new XSSFWorkbook(resource);
            System.out.println(excel);
            //填充数据
            XSSFSheet sheet = excel.getSheet("人员成绩信息表");


           Row row = sheet.getRow(0);
            int i = 2;
            for (ScoreInfo scoreInfo:scoreInfoList){

                row = sheet.createRow(i++);
                System.out.println(scoreInfo.getStudentId());
                row.createCell(0).setCellValue(scoreInfo.getStudentId());
                row.createCell(1).setCellValue(scoreInfo.getStuName());
                row.createCell(2).setCellValue(scoreInfo.getStuSex());
                row.createCell(3).setCellValue(scoreInfo.getStuDepartment());
                row.createCell(4).setCellValue(scoreInfo.getStuGrade());
                row.createCell(5).setCellValue(scoreInfo.getYear()+"级");
                row.createCell(6).setCellValue(scoreInfo.getStuSpeciality());
                row.createCell(7).setCellValue(scoreInfo.getClassNumber()+"班");
                if (!StringUtils.isEmpty(scoreInfo.getExamScore()))
                    row.createCell(8).setCellValue(scoreInfo.getExamScore());
                if (!StringUtils.isEmpty(scoreInfo.getIntvScore()))
                    row.createCell(9).setCellValue(scoreInfo.getIntvScore());

            }
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);

            outputStream.close();
            excel.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 导出入班学生表
     * @param response
     */
    public void exportEnterTable(HttpServletResponse response,String className) {
        List<IntvInfo> enterInfoList = excelExportMapper.getExportEnterTable(className);

        InputStream resource = this.getClass().getClassLoader().getResourceAsStream("excelTemplate/入班人员信息表模板.xlsx");

        //基于模板文件新建一个新的excel文件

        try {
            XSSFWorkbook excel = new XSSFWorkbook(resource);
            System.out.println(excel);
            //填充数据
            XSSFSheet sheet = excel.getSheet("入班人员信息表");


//            row.getCell(0).setCellValue(1+"入班人员信息表");
            int i = 2;
            for (IntvInfo enterInfo:enterInfoList){
                Row row = sheet.getRow(0);
                row.getCell(0).setCellValue(enterInfo.getClassName()+"入班人员信息表");
                row = sheet.createRow(i++);
                System.out.println(enterInfo.getStudentId());
                row.createCell(0).setCellValue(enterInfo.getStudentId());
                row.createCell(1).setCellValue(enterInfo.getStuName());
                row.createCell(2).setCellValue(enterInfo.getStuSex());
                row.createCell(3).setCellValue(enterInfo.getStuDepartment());
                row.createCell(4).setCellValue(enterInfo.getStuGrade());
                row.createCell(5).setCellValue(enterInfo.getYear()+"级");
                row.createCell(6).setCellValue(enterInfo.getStuSpeciality());
                row.createCell(7).setCellValue(enterInfo.getClassNumber()+"班");
                row.createCell(8).setCellValue(enterInfo.getUpdateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);

            outputStream.close();
            excel.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
